package cn.jbolt.ndemo;

import cn.jbolt.ai.app.AppType;
import cn.jbolt.ai.app.entity.AiApp;
import cn.jbolt.ai.app.entity.AiAppConfig;
import cn.jbolt.ai.app.entity.AiAppNode;
import cn.jbolt.ai.app.service.AiAppConfigService;
import cn.jbolt.ai.app.service.AiAppNodeService;
import cn.jbolt.ai.app.service.AiAppService;
import cn.jbolt.ai.core.chain.node.InnerNodeType;
import cn.jbolt.ai.core.chain.node.base.AppConfigConstant;
import cn.jbolt.ai.core.chain.node.start.StartFromUserInputConfig;
import cn.jbolt.util.JSONUtil;
import cn.jbolt.util.Result;
import com.jboltai.event.chain.ChainNode;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import cn.jbolt.ai.core.chain.node.NodeProviderCenter;

import java.util.ArrayList;
import java.util.List;

@Component
public class DemoAppIniter {

    private AiAppService aiAppService;
    private AiAppConfigService aiAppConfigService;
    private AiAppNodeService aiAppNodeService;

    public DemoAppIniter(AiAppService aiAppService, AiAppConfigService aiAppConfigService, AiAppNodeService aiAppNodeService) {
        this.aiAppService = aiAppService;
        this.aiAppConfigService = aiAppConfigService;
        this.aiAppNodeService = aiAppNodeService;
    }


    @EventListener(ApplicationReadyEvent.class)
    public void init() {
        //1. 注册特殊的节点类型
        NodeProviderCenter.registerCommonNodeProvider(DemoAppConstrant.EXTRACT_NODE_TYPE, FileExtractorNode.class);
        NodeProviderCenter.registerCommonNodeProvider(DemoAppConstrant.TEXT_NODE_TYPE, TextExtractorNode.class);
        //2. 初始化AI应用
        checkAndInitApp();
    }

    private void checkAndInitApp() {
        AiApp app = aiAppService.getById(DemoAppConstrant.APP_ID);
        if (app != null) {
            return;
        }
        app = new AiApp();
        app.setId(DemoAppConstrant.APP_ID);
        app.setBuildIn(false);
        app.setName(DemoAppConstrant.APP_NAME);
        app.setSn(DemoAppConstrant.APP_SN);
        app.setBriefInfo(DemoAppConstrant.APP_BRIEF_INFO);
        app.setAppType(AppType.SIMPLE.getCode());
        app.setEnable(true);
        aiAppService.save(app);

        aiAppService.initAppConfig(app);
        initAppNodes(app);
    }

    /**
     * 初始化应用的节点
     * @param app
     */
    private void initAppNodes(AiApp app) {
        //开始节点
        AiAppNode startNode = new AiAppNode();
        startNode.setType(InnerNodeType.START_FROM_USERINPUT);
        startNode.setAppId(app.getId());
        aiAppNodeService.add(startNode);
        //第一个节点
        AiAppNode extractNode = new AiAppNode();
        extractNode.setAppId(app.getId());
        extractNode.setType(DemoAppConstrant.EXTRACT_NODE_TYPE);
        aiAppNodeService.add(extractNode);
        //第二个节点
        AiAppNode textNode = new AiAppNode();
        textNode.setAppId(app.getId());
        textNode.setType(DemoAppConstrant.TEXT_NODE_TYPE);
        aiAppNodeService.add(textNode);

        aiAppNodeService.addNodeAfters(startNode.getId(), ChainNode.AFTER_KEY_SUCCESS, extractNode.getId());
        aiAppNodeService.addNodeAfters(extractNode.getId(), ChainNode.AFTER_KEY_SUCCESS, textNode.getId());
        initNodeConfig(startNode.getId());



    }
    private Result initNodeConfig(Long startNodeId) {
        List<AiAppConfig> configs = new ArrayList<>();
        AiAppConfig startNodeConfig = new AiAppConfig();

        //开始节点配置
        startNodeConfig.setLinkType(AppConfigConstant.LINK_TYPE_NODE);
        startNodeConfig.setLinkId(startNodeId);
        StartFromUserInputConfig startInputNode = new StartFromUserInputConfig();
        startInputNode.setOutputField("prompt");
        startNodeConfig.setConfigValue(JSONUtil.toJson(startInputNode));
        configs.add(startNodeConfig);


        return aiAppConfigService.addConfigs(configs);

    }



}
